import "@src/common/initTheme"

/** 各入口文件应该引入该文件替代手动引入 */
import '@src/common/polyfill';
import '@src/modules/linkc/assets/public.scss';
// import 'publink-ui/lib/publink-ui.css';
import '@src/common/init'
import '@src/common/table.js'

import Vue from 'vue';
import i18n from '@src/locales'
import http from '@src/util/http'

import VueTour from 'vue-tour'
import 'vue-tour/dist/vue-tour.css'
Vue.use(VueTour)

import component from '@src/component';
import directive from '@src/directive';
import filter from '@src/filter';
import BaseGallery from 'packages/BaseGallery';

import { customerOperateLog } from '@src/util/operatelog';// 用户操作日志
import appConfig from 'app.config';
import platform from '@src/platform';
import platformExtension from '@src/platform/extension'
import { getRootWindow, removeClassByRegExp } from '@src/util/dom'
import { isQYWX, isOpenData, isDevelopment } from '@src/util/platform';
import globalMixin from '@src/mixins/globalMixin'
import { Fragment } from 'vue-frag'

import { setAuthClass } from '@src/util/auth'

import { RESOURCE_PREFIX } from '@src/util/linkSwitch'; 
import { userCenterGetTokenInfo } from '@src/util/userCenter';
import { formatTrackerParams, eventTrackURL } from '@src/util/tracker/common';
import { chartTrack, chartExportTrack } from '@src/util/chartsFn'


import { useMapType } from '@src/util/map';
// 引入埋点
import MonitorWeb from '@src/util/tracker/tracker';
const rootWindow = getRootWindow(window);
const { token } = userCenterGetTokenInfo();

if(!rootWindow.publinkTracker) {
  // 防止重复初始化tracker
  rootWindow.publinkTracker = new MonitorWeb({
    url: eventTrackURL(), // 埋点接口url, 不传则默认使用日志上报url
    maxRetryCount: 2, // 上报重试次数
    reportingCycle: 10000, // 上报周期, 单位:毫秒
    isLog: true, //是否打印日志
    token // 可传可不传
  });
  rootWindow.publinkTracker.formatTrackerParams = formatTrackerParams;
  rootWindow.publinkTracker.clickStat = isDevelopment() ? (() => {}) : rootWindow.publinkTracker.clickStat;
  rootWindow.publinkTracker.formatParams = (id, data, secondId) => {
    return formatTrackerParams({ id, data, secondId });
  };
  rootWindow.publinkTracker.commonFn = {
    // 报表用
    chartTrack,
    chartExportTrack
  }
}
//安装组件
Vue.component('fragment', Fragment)
Vue.component('Fragment', Fragment)
Vue.use(directive);
Vue.use(filter);
Vue.use(component);
Vue.mixin(globalMixin)


import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
import '@src/assets/scss/index.scss';
import '@src/assets/icon/iconfont.js'

import previeVideoCompoment from '@src/component/compomentV2/PreviewVideo/index.vue';
import previePDFCompoment from '@src/component/compomentV2/PreviePDF/index.vue';
import previeFielCompoment from '@src/component/compomentV2/PreviewFile/PreviewFile.vue'
import PrevieElementImgCompoment from '@src/component/compomentV2/PrevieElementImg/index.vue';
import WWOpenData from '@src/component/common/WWOpenData/WWOpenData.vue';
// 打开富文本
import BaseViewRichText from 'src/component/common/BaseViewRichText/BaseViewRichText.vue'

/** utils */
import { getThemeClass } from '@src/util/theme.ts';
import domGuide from '@src/common/Guide'

/* 异常监控 */
import { errorHandler, production } from '@src/util/onerror/index.js'

// if(production()){
//   Vue.config.errorHandler = errorHandler
// }

/* 微信通讯录组件本地开发环境兼容 */
if (isDevelopment() && isOpenData) {
  Vue.component(WWOpenData.name, WWOpenData)
}

function previewImg(url) {
  if (!url) return;
  if (url.target && url.target.nodeName == 'IMG')
    return BaseGallery.preview(url.target);
  let imgDom = document.createElement('img');
  imgDom.src = url;
  BaseGallery.preview(imgDom);
}

/**
 * 使用Element image-view预览图片
 * @param {Array} url 图片地址
*/
export function previewElementImg(url = '', urls = [], info = {}) {
  let PrevieElementImg = Vue.extend(PrevieElementImgCompoment);

  let divDom = document.createElement('div');
  divDom.id = 'normal-preview-ElementImg';
  divDom.ref = 'previewElementImg';
  if (!document.getElementById('normal-preview-ElementImg')) {
    // this.$el.appendChild(divDom);
    document.body.appendChild(divDom)
  }

  new PrevieElementImg({
    i18n,
    data() {
      return {};
    },
    propsData: {
      url,
      urls,
      info,
    },
    methods: {},
  }).$mount('#normal-preview-ElementImg');
}

/**
 * 
 * @param {String} url 视频地址
 * @param {Object} info 视频详细信息
*/
function previewVideo(url, info = {}) {
  let PrevieVideoCompoments = Vue.extend(previeVideoCompoment);
  let divDom = document.createElement('div');
  divDom.id = 'normal-preview-video';
  divDom.ref = 'previeVideoCompoment';
  if (!document.getElementById('normal-preview-video')) {
    document.body.appendChild(divDom);
  }

  // }
  new PrevieVideoCompoments({
    i18n,
    data() {
      return {};
    },
    propsData: {
      url,
      info
    },
    methods: {},
  }).$mount('#normal-preview-video');
}

/**
 * 
 * @param {String} url PDF地址
 * @param {Object} info PDF详细信息
*/
function previewPDF(url, info = {}) {
  let previePDFCompoments = Vue.extend(previePDFCompoment);

  let divDom = document.createElement('div');
  divDom.id = 'normal-preview-pdf';
  divDom.ref = 'previePDFCompoment';
  if (!document.getElementById('normal-preview-pdf')) {
    document.body.appendChild(divDom);
  }

  new previePDFCompoments({
    i18n,
    data() {
      return {};
    },
    propsData: {
      url,
      info
    },
    methods: {},
  }).$mount('#normal-preview-pdf');
}

/**
 *
 * @param {Object} info 文件详细信息
 */
function previewFile(info) {
  let previeFielCompoments = Vue.extend(previeFielCompoment);

  let divDom = document.createElement('div');
  divDom.id = 'normal-preview-file';
  divDom.ref = 'previeFielCompoment';
  if (!document.getElementById('normal-preview-file')) {
    document.body.appendChild(divDom);
  }

  // eslint-disable-next-line new-cap
  new previeFielCompoments({
    i18n,
    data() {
      return {};
    },
    propsData: {
      info,
    },
    methods: {},
  }).$mount('#normal-preview-file');
}

function getUrlObj(window_) {
  let params = window_.location.href.split('?')[1];
  let obj = {};
  try {
    if (params) {
      params.split('&').forEach((item) => {
        let item_ = item.split('=');
        obj[item_[0]] = item_[1];
      });
    }
  } catch (error) {
    console.warn(error);
  }
  return obj;
}

export function openViewRichText(richtextId) {
  const BaseViewRichTextComponents = Vue.extend(BaseViewRichText);
  const instance = new BaseViewRichTextComponents().$mount();
  document.body.appendChild(instance.$el);
  instance?.openDialog(richtextId);
}

Vue.prototype.$http = http;
Vue.prototype.$appConfig = appConfig;
Vue.prototype.$platform = platform;
Vue.prototype.$previewImg = previewImg;
Vue.prototype.$previewElementImg = previewElementImg;
Vue.prototype.$previewVideo = previewVideo;
Vue.prototype.$previewPDF = previewPDF;
Vue.prototype.$previewFile = previewFile
Vue.prototype.$getUrlObj = getUrlObj;
Vue.prototype.$Guide = domGuide;
Vue.prototype.$resourcePrefix = appConfig.env == 'development' ? '' : RESOURCE_PREFIX;
const eventBus = new Vue();
Vue.prototype.$eventBus = eventBus;
Vue.prototype.$track = rootWindow.publinkTracker;
Vue.prototype.$safeParams = function (params) {
  return params;
}
Vue.prototype.$customerOperateLog = customerOperateLog;
Vue.prototype.$openViewRichText = openViewRichText;


// 主题设置 todo: 还是有明显的切换
(async function () {
  let themeClass = await getThemeClass();
  let body = document.getElementsByTagName('body')[0];
  if (body) {
    removeClassByRegExp(body, /theme-*/);
    body.classList.add(themeClass);
  }
})();

let body = document.getElementsByTagName('body')[0];
if (body) {
  body.className = `${body.className} theme-default`
}


(async function () {
  const { initSytemMap } = await useMapType()
  await initSytemMap()
})();
// 微信配置
// if (window.wx || isQYWX()) {
//   const rootWindow = getRootWindow(window)
//   if (rootWindow.wxConfig) {
//     // platformExtension.injectAuth(rootWindow.wxConfig)
//   } else {
//     platformExtension.sign().then(()=>{
//       eventBus.$emit('wx-sign-done')
//     })
//   }
// }

setAuthClass()

export default Vue;
